<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class admin_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Admin_model');
		$this->CI->load->model('Role_model');
		$this->CI->load->library('ProjectService/role_service');
		$this->CI->load->library('ProjectService/shop_service');
	}

	/**  
	* 获取客户账户
	* @access public 
	* @param  $param cond参数
	* @param  $del
	*/  
	public function getAdminByParam($param,$del=false) {
		if(!$del){
			$param['status']=CommonStatus::UnDeleted[XPHEnum::Value];
		}
		$admin=$this->CI->Admin_model->get_single($param);
		return $admin;
	}

	/**  
	* 获取客户账户
	* @access public 
	* @param  $park_id 客户id
	* @param  $is_park_default
	*/  
	public function getAdminByParkNotInRole($park_id,$is_park_default) {
		$role=$this->CI->role_service->getRoleByPark($park_id,$is_park_default);
		// var_dump($this->CI->db->last_query());
		// exit();
		if($role){
			$role_id_array=array_column($role, 'id');
			$sql=$this->CI->sqlLikeEscape(" select * from db_admin where role_id not in (??) and park_id=?? ",array(implode(',',$role_id_array),$park_id));
			$admin=$this->CI->Admin_model->query($sql);
			// var_dump($this->CI->db->last_query());
			// exit();
			return $admin;
		}
		else{
			return false;
		}
		
	}


	/**  
	* 验证当前用户是否为管理员用户（admin，manage）
	* @access public 
	* @param  $admin_id
	* @return  bool 
	*/  
	public function checkAdminManage($admin_id) {
		$admin=$this->getAdminByParam(array('id'=>$admin_id),true);
		$role=$this->CI->role_service->getRoleByCond(array('id'=>$admin['role_id']));
		if($role['is_park_default']==CommonWhether::Whether[XPHEnum::Value]){
			return true;
		}
		else{
			return false;
		}
	}


	/**  
	* 根据admin_id判断所属门店为多个还是单个
	* @access public 
	* @param  $admin_id
	* @return  bool 
	*/  
	public function checkSingleShop($admin_id) {
		$admin=$this->CI->Admin_model->get_single(array('id'=>$_POST['admin_id']));
		if(!isNullOrEmpty($admin['sub_role_id'])){
			$role_id_str=$admin['role_id'].','.$admin['sub_role_id'];
		}
		else{
			$role_id_str=$admin['role_id'];
		}
		// var_dump($role_id_str);
		// exit();
		$single=true;
		foreach (explode(',',$role_id_str) as $k => $v) {
			$role=$this->CI->role_service->getRoleByCond(array('id'=>$v));
			if($role['enum_id']==DefaultParkWebRole::Dealer[XPHEnum::Value] || $role['enum_id']==DefaultParkWebRole::BrandManager[XPHEnum::Value]){
				$single=false;
			}
		}
		return $single;
	}

	/**  
	* 获取指定角色的role_id
	* @access public 
	* @param  $park_id 客户id
	* @param  $enum_id_array
	* @return  array 
	*/  
	public function getParkRoleIdByEnumId($park_id,$enum_id_array) {
		$role=$this->CI->role_service->getRoleListByCond(array('park_id'=>$park_id));
		$role_id_array=array();
		foreach ($role as $k => $v) {
			if(in_array($v['enum_id'],$enum_id_array)){
				$role_id_array[]=$v['id'];
			}
		}
		return $role_id_array;
	}

	/**
	* 获取指定角色id
	* @access public
	* @param  $park_id，$role 角色类型
	* @return  int
	*/
	public function getRoleId($park_id,$role_id) {
		$data=$this->CI->Role_model->get_single(array('park_id'=>$park_id,'id'=>$role_id));
		return $data;
	}

	/**  
	* 获取指定角色的admin_id数组
	* @access public 
	* @param  $park_id 客户id
	* @param  $enum_id_array
	* @return  array 
	*/  
	public function getParkRoleAdminIdByEnumId($park_id,$enum_id_array) {
		$role_id_array=$this->getParkRoleIdByEnumId($park_id,$enum_id_array);
		// var_dump($role_id_array);
		// exit();
		if(count($role_id_array)>0){
			$connar['where']=$this->CI->sqlEscape(" park_id=?? and status=?? and (find_in_set(role_id,??) or F_inarray(sub_role_id,??)=1) ",array($park_id,CommonStatus::UnDeleted[XPHEnum::Value],implode(',',$role_id_array),implode(',',$role_id_array)));
			$admin=$this->CI->Admin_model->get_list_full($connar);
			return $admin;
		}
		else{
			return array();
		}
		
	}



	/**  
	* 获取账户所拥有的店铺权限
	* @access public 
	* @param  $admin_id 账户id
	* @return  array shopid数组
	*/  
	public function getAdminShopPermission($admin_id) {
		$admin=$this->getAdminByParam(array('id'=>$admin_id));
		if(!$admin){
			return array();
		}
		$park_id=$admin['park_id'];

		//用户角色数组
		$role_id_str=$admin['role_id'].','.$admin['sub_role_id'];
		$role_id_array=explode(',',$role_id_str);
		foreach ($role_id_array as $k => $v) {
			if(isNullOrEmpty($v)){
				unset($role_id_array[$k]);
			}
		}
		//通过role_id获取enum_id
		if(count($role_id_array)==0){
			$enum_id_array=array();
		}
		else{
			$connar['where']=$this->CI->sqlEscape(" 1=1 and find_in_set(id,??) and park_id=?? ",array(implode(',',$role_id_array),$park_id));
			$role=$this->CI->Role_model->get_list_full($connar);
			if($role){
				$enum_id_array=array_column($role,'enum_id');
			}
			else{
				$enum_id_array=array();
			}
		}

		//店员
		if(in_array(DefaultParkWebRole::Clerk[XPHEnum::Value],$enum_id_array)){
			$shop1=explode(',',$admin['shop_id']);
		}
		else{
			$shop1=array();
		}
		//店长
		if(in_array(DefaultParkWebRole::Shopowner[XPHEnum::Value],$enum_id_array)){
			$shop2=explode(',',$admin['shop_id']);
		}
		else{
			$shop2=array();
		}
		//品牌经理
		if(in_array(DefaultParkWebRole::BrandManager[XPHEnum::Value],$enum_id_array)){
			$shop3=explode(',',$admin['shop_id']);
		}
		else{
			$shop3=array();
		}
		//经销商老板
		if(in_array(DefaultParkWebRole::Dealer[XPHEnum::Value],$enum_id_array)){
			$shop4=explode(',',$admin['shop_id']);
		}
		else{
			$shop4=array();
		}
		//华南销售
		if(in_array(DefaultParkWebRole::HN_Sales[XPHEnum::Value],$enum_id_array)){
			$shop=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::South[XPHEnum::Value]));
			if($shop){
				$shop5=array_column($shop,'id');
			}
			else{
				$shop5=array();
			}
		}
		else{
			$shop5=array();
		}
		//华东销售
		if(in_array(DefaultParkWebRole::HD_Sales[XPHEnum::Value],$enum_id_array)){
			$shop=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::East[XPHEnum::Value]));
			if($shop){
				$shop6=array_column($shop,'id');
			}
			else{
				$shop6=array();
			}
		}
		else{
			$shop6=array();
		}
		//华北销售
		if(in_array(DefaultParkWebRole::HB_Sales[XPHEnum::Value],$enum_id_array)){
			$shop=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::North[XPHEnum::Value]));
			if($shop){
				$shop7=array_column($shop,'id');
			}
			else{
				$shop7=array();
			}
		}
		else{
			$shop7=array();
		}
		//POS销售总监
		if(in_array(DefaultParkWebRole::POS_SalesDirector[XPHEnum::Value],$enum_id_array)){
			$_shop1=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::South[XPHEnum::Value]));
			$_shop2=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::East[XPHEnum::Value]));
			$_shop3=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::North[XPHEnum::Value]));
			$_shop=array_merge($_shop1,$_shop2,$_shop3);
			if(count($_shop)==0){
				$shop8=array();
			}
			else{
				$shop8=array_column($_shop,'id');
			}
		}
		else{
			$shop8=array();
		}
		//DOS销售助理&&DOS销售总监
		if(in_array(DefaultParkWebRole::DOS_SalesAssistant[XPHEnum::Value],$enum_id_array)||in_array(DefaultParkWebRole::DOS_SalesDirector[XPHEnum::Value],$enum_id_array)){
			$_shop=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id,'region'=>SalesArea::Dos[XPHEnum::Value]));
			if(count($_shop)==0){
				$shop9=array();
			}
			else{
				$shop9=array_column($_shop,'id');
			}
		}
		else{
			$shop9=array();
		}
		//产品部&&市场部&&总经理&&管理员
		if(in_array(DefaultParkWebRole::Product[XPHEnum::Value],$enum_id_array)||in_array(DefaultParkWebRole::Market[XPHEnum::Value],$enum_id_array)||in_array(DefaultParkWebRole::Boss[XPHEnum::Value],$enum_id_array)||in_array(DefaultParkWebRole::Manage[XPHEnum::Value],$enum_id_array)){
			$_shop=$this->CI->shop_service->getShopListByCond(array('park_id'=>$park_id));
			if(count($_shop)==0){
				$shop10=array();
			}
			else{
				$shop10=array_column($_shop,'id');
			}
		}
		else{
			$shop10=array();
		}

		$last_shop=array_merge($shop1,$shop2,$shop3,$shop4,$shop5,$shop6,$shop7,$shop8,$shop9,$shop10);
		foreach ($last_shop as $k => $v) {
			if(isNullOrEmpty($v)){
				unset($last_shop[$k]);
			}
		}
		$last_shop=array_values($last_shop);
		return $last_shop;
	}

}